/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package com.simplemonkey.widgets;

import com.jme.image.Texture;
import com.jme.scene.SceneElement;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.simplemonkey.Container;
import com.simplemonkey.IResizeListener;
import com.simplemonkey.util.Dimension;

public class TextureContainer extends Container {
	
	protected Quad background;
	private TextureState backgroundTexture;
	
	public TextureState getBackgroundTexture() {
		return backgroundTexture;
	}

	public Spatial getBackgroundSpatial() {
		return background;
	}
	
	public TextureContainer() {
		super();
		super.setPermeable(false);
		background = new Quad("TextureWidget",1,1);
		
		background.getLocalScale().set(getWidth(),getHeight(),1f);
		background.getLocalTranslation().set(getWidth()/2f,getHeight()/2f,1f);
		background.setCullMode(SceneElement.CULL_INHERIT);
		backgroundTexture = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		
		background.copyTextureCoords(0, 0, 1);
		background.setZOrder(1024);
		background.setRenderState(backgroundTexture);

		getSpatial().attachChild(background);
		getSpatial().updateWorldVectors();
		getSpatial().updateRenderState();
		background.updateRenderState();
		
		super.addResizeListener(new IResizeListener()
		{

			
			public void resize(Dimension newSize) {
				Dimension size = new Dimension(getSpacingAppearance().getContentSize());
				
				background.getLocalScale().set(size.getWidth()+getSpacingAppearance().getPadding().getLeftPlusRight(),size.getHeight()+getSpacingAppearance().getPadding().getBottomPlusTop(),1f);
				background.getLocalTranslation().set(getWidth()/2f,getHeight()/2f,1f);
				getSpatial().updateGeometricState(0, true);
			}
			
		});
	}

	public void setTexture(Texture texture, int unit)
	{
		backgroundTexture.setTexture(texture,unit);
		background.updateRenderState();
	}
	
	public void setTexture(Texture texture)
	{
		this.setTexture(texture,0);
	}
	



	
}
